package cn.gson.crm.controller.follow;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.gson.crm.controller.BaseServlet;
import cn.gson.crm.model.dao.CustomerDao;
import cn.gson.crm.model.dao.FollowDao;
import cn.gson.crm.model.entity.Dept;
import cn.gson.crm.model.entity.Employee;
import cn.gson.crm.model.entity.Follow;
import cn.gson.crm.model.entity.Way;

/**
 * Servlet implementation class FollowManager
 */
@WebServlet("/follow/manager")
public class FollowManager extends BaseServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void get(HttpServletRequest request, HttpServletResponse response) throws Exception {
		commonMethod(request, response);
		forward("follow/follow_manager.jsp");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void post(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String custId = request.getParameter("custId");
		String empId = request.getParameter("empId");
		String wayId = request.getParameter("wayId");
		String situation = request.getParameter("situation");
		
		if(wayId == null || wayId.isEmpty()){
			commonMethod(request, response);
			errorForward("请选择跟进方式！", "follow/follow_manager.jsp");
			return;
		}
		if(situation == null || situation.isEmpty()){
			commonMethod(request, response);
			errorForward("跟进内容不可为空！", "follow/follow_manager.jsp");
			return;
		}
		
		Follow f = new Follow();
		f.setFollow_custname_id(Integer.parseInt(custId));
		f.setFollow_situation(situation);
		f.setFollow_way_id(Integer.parseInt(wayId));
		f.setFollow_emp_id(Integer.parseInt(empId));
		
		new FollowDao().insertFollowInfo(f);
		redirect("cust/form");
	}

	private void commonMethod(HttpServletRequest request, HttpServletResponse response) throws Exception {
		Employee e = (Employee) request.getSession().getAttribute("emp");
		request.setAttribute("emp", e);
		String id = request.getParameter("id");
		request.setAttribute("id", id);
		String name = request.getParameter("name");
		request.setAttribute("name", name);
		List<Way> listWay = new CustomerDao().getAllWay();
		request.setAttribute("listWay", listWay);
	}
}
